import {storeBindingsBehavior} from 'mobx-miniprogram-bindings'
import {queryDiscount, queryMemberCouponDiscount} from "@src/api/order";
import base from "@src/store/base";
import router from "@src/router";
import {discountMd5} from "@src/common/discountUtils";

Component({
  behaviors: [storeBindingsBehavior],
  storeBindings: [{
    store: base,
    fields: ['themeColor', 'themeTitleColor'],
  }],
  properties: {
    activityShow:{
      type: Boolean,
      value:false
    },
    zIndex: {
      type: Number,
      value: 5000
    },
    activeList:{
      type:Object,
      value:[],
    },
    chooseInfo:{
      type:Object,
      value:{}
    }
  },
  data: {
    selected: null,
    discountInfo: {},
    // discountList: [],
    type: null,
    //content高度
    contentHeight: 20,
    title:"优惠",
    activeList:[]
  },

  options: {
    multipleSlots: true,
    addGlobalClass: true
  },
  methods: {
    //设置spec组件隐藏
    closeDialog(e) {
      console.log(this.data.chooseInfo)
      if (this.data.chooseInfo === null ){
        this.setData({selected:null})
      }else{
        this.setData({selected:this.data.chooseInfo.selectContId})
      }
      if(e.currentTarget.dataset.area){
        this.triggerEvent("LevelCommentBtn", null)
        this.setData({selected: null, discountInfo: null});
      }
      this.setData({ activityShow: false })


    },
    //监听高度
    handleContentHeight(e) {
      this.setData({ contentHeight: e.detail })
    },
    //点击不同方案切换时事件
    handleSelect(e) {
      let index = e.currentTarget.dataset.index;
      console.log(index,this.data.selected)
      if (index === this.data.selected) {
        this.setData({selected: null, discountInfo: null});
        return;
      }
      this.setData({selected: index, discountInfo: this.data.activeList[index]});
    },
//点击确定的事件
    handleConfirm() {
      const eventChannel = this.getOpenerEventChannel()
      if (eventChannel) {
        eventChannel.emit('change', this.data.discountInfo);
      }
      const activeList = this.data.activeList[this.data.selected];
      activeList.selectContId = this.data.selected
      this.triggerEvent("LevelCommentBtn", activeList)
      this.setData({activityShow:false})
    }
  },
  lifetimes:{
    attached() {
      if(this.data.chooseInfo.selectContId !=undefined || this.data.chooseInfo.selectContId !=null){
        this.setData({selected:this.data.chooseInfo.selectContId})
      }
0    }
  }
    // onLoad(options) {
    //   debugger
    //   let {shopId, totalFee, selectItem, orderType, type} = options;
    //   this.setData({shopId, totalFee, selectItem, orderType, type});
    //   // this.fetchData();
    //   let discountList = this.data.activeList;
    //       discountList.sort((item1,item2)=>{
    //         if (item1-item2<0){return -1}
    //       })
    //   console.log(discountList)
    //       for (let i = 0; i < discountList.length; i++) {
    //         let discountInfo = discountList[i];
    //         if (this.data.selectItem && this.data.selectItem === discountMd5(discountInfo)) {
    //           this.setData({discountInfo: discountInfo, selected: i, discountList: discountList})
    //           return;
    //         }
    //       }
    //       this.setData({selected: null, discountInfo: null, discountList: discountList})
    // },
    // fetchData() {
    //   let data = {
    //     orderType: this.data.orderType,
    //     shopId: this.data.shopId,
    //     tradeFee: this.data.totalFee
    //   }
    //   let callFun = queryDiscount;
    //
    //   if (this.data.type && this.data.type === 'memberCoupon') {
    //     callFun = queryMemberCouponDiscount
    //   }

      // callFun(data).then(res => {
      //   if (res.code === 0) {
      //     let discountList = res.data
      //     for (let i = 0; i < discountList.length; i++) {
      //       let discountInfo = discountList[i];
      //       if (this.data.selectItem && this.data.selectItem === discountMd5(discountInfo)) {
      //         this.setData({discountInfo: discountInfo, selected: i, discountList: discountList})
      //         return;
      //       }
      //     }
      //     this.setData({selected: null, discountInfo: null, discountList: discountList})
      //   }
      // })
    // },


})
